// 反转 “i love china”
var str = "i love china"
var arr = ['']
for (i = 0, j = 0; i < str.length; i++) {
    if (str[i] != ' ') {
        arr[j] += str[i]
    } else {
        j++
        arr[j] = ''
    }
}

var str1 = ''
for (i = arr.length - 1; i >= 0; i--) {
    str1 += arr[i] + ' '
}

console.log(str1);

// 重构every方法
Array.prototype.myEvery = function (callback) {
    for (let i = 0; i < this.length; i++) {
        if (!callback(this[i], i, this)) {
            return false
        }
    }
    return true
}

var arr = [1, 2, 3, 4, 5]
var res = arr.myEvery(function (item, index, arr) {
    return item > 1
})

console.log(res);

// 重构some方法
Array.prototype.mySome = function (callback) {
    for (let i = 0; i < this.length; i++) {
        if (callback(this[i], i, this)) {
            return true
        }
    }
    return false
}

var arr = [1, 2, 3, 4, 5]
var res = arr.mySome(function (item, index, arr) {
    return item > 3
})

console.log(res);

// 重构filter方法
Array.prototype.myFilter = function (callback) {
    var newArr = []
    for (let i = 0; i < this.length; i++) {
        if (callback(this[i], i, this)) {
            newArr[newArr.length] = this[i]
        }
    }
    return newArr
}

var arr = [1, 2, 3, 4, 5]
var res = arr.myFilter(function (item, index, arr) {
    return item > 3
})

console.log(res);

// 重构map方法
Array.prototype.myMap = function (callback) {
    var newArr = []
    for (let i = 0; i < this.length; i++) {
        newArr[newArr.length] = callback(this[i])
    }
    return newArr
}

var arr = [1, 2, 3, 4, 5]
var res = arr.myMap(function (item, index, arr) {
    return item + 5
})

console.log(res);